//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  ChoosePets.m
//  DingDing
//
//  Created by LHL on 14/11/24.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "ChoosePets.h"
#import "ChoosePetsCell.h"
#import "MJRefresh.h"
#pragma mark -

@interface ChoosePets()
{
	//<#@private var#>
}
@end

@implementation ChoosePets{
//    UISegmentedControl *seg;
//    NSMutableArray *currentArr;
   __weak UITableView *_tableView;
    NSMutableArray *_dataArray;
    UIView *titleView;
    NSInteger currentindex;
}

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
//DEF_OUTLET(BeeUIScrollView, list)
DEF_MODEL(getPetList, igetPetList)
- (void)load
{
    _dataArray = [[NSMutableArray alloc]init];
    self.igetPetList = [getPetList modelWithObserver:self];
    titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 29)];
    currentindex = 0;
}

- (void)unload
{
}

-(getPetList *)iigetPetList{
    return self.igetPetList;
}

ON_SIGNAL3(getPetList, RELOADED, SIGNAL){
    [self dismissTips];
    [_tableView headerEndRefreshing];
    [self cacDataArray];
}

ON_SIGNAL3(getPetList, RELOADING, SIGNAL){
    [self presentLoadingTips:@"加载中"];
}

-(void)cacDataArray{
    [_dataArray removeAllObjects];
    NSString *temp = @"";
    petsModel *pet = [[petsModel alloc]init];
    NSMutableArray *currentArr = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self iigetPetList].petlist.dog.count; i++) {
        PETMODEL *model = [self iigetPetList].petlist.dog[i];
        if ([temp isEqualToString:@""]) {
            temp = model.index;
        }
        if (![temp isEqualToString:model.index] || i == [self iigetPetList].petlist.dog.count-1) {
            pet.key = temp;
            [pet.data addObject:model];
            [currentArr addObject:pet];
            pet = [[petsModel alloc]init];
            temp = model.index;
        }else{
            pet.key = model.index;
            [pet.data addObject:model];
        }
    }
    [_dataArray addObject:[NSMutableArray arrayWithArray:currentArr]];
    currentArr = nil;
    currentArr = [[NSMutableArray alloc]init];
    temp=@"";
    for (int i = 0; i < [self iigetPetList].petlist.cat.count; i++) {
        PETMODEL *model = [self iigetPetList].petlist.cat[i];
        if ([temp isEqualToString:@""]) {
            temp = model.index;
        }
        if (![temp isEqualToString:model.index] || i == [self iigetPetList].petlist.cat.count-1) {
            pet.key = temp;
            [pet.data addObject:model];
            [currentArr addObject:pet];
            pet = [[petsModel alloc]init];
            temp = model.index;
        }else{
            pet.key = model.index;
            [pet.data addObject:model];
        }
    }
    [_dataArray addObject:[NSMutableArray arrayWithArray:currentArr]];
    currentArr = nil;
    currentArr = [[NSMutableArray alloc]init];
    temp=@"";
    for (int i = 0; i < [self iigetPetList].petlist.other.count; i++) {
        PETMODEL *model = [self iigetPetList].petlist.other[i];
        if ([temp isEqualToString:@""]) {
            temp = model.index;
        }
        if (![temp isEqualToString:model.index] || i == [self iigetPetList].petlist.other.count-1) {
            pet.key = temp;
            [pet.data addObject:model];
            [currentArr addObject:pet];
            pet = [[petsModel alloc]init];
            temp = model.index;
        }else{
            pet.key = model.index;
            [pet.data addObject:model];
        }
    }
    [_dataArray addObject:[NSMutableArray arrayWithArray:currentArr]];
    currentArr = nil;
    if (_dataArray.count>0) {
        [_tableView reloadData];
    }
}


#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    
    self.navigationBarShown = YES;
//    seg = [[UISegmentedControl alloc]initWithItems:@[@"狗狗",@"猫咪",@"其它"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"www.baidu.com"]];
    NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    [connect start];
    
    self.segment = [[HMSegmentedControl alloc]init];
    [self.segment setFrame:CGRectMake(0, 0, 227, 30)];
    NSArray *arr = @[@"狗狗",@"猫咪",@"其它"];
    self.segment.backgroundColor =RGB(203, 74, 69);
    [self.segment setSelectedSegmentIndex:0];
    [self.segment setTextColor:[UIColor whiteColor]];
    [self.segment setFont:[UIFont systemFontOfSize:13]];
    [self.segment setSelectedTextColor:RGB(203, 74, 69)];
    titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 29)];
    for (int i = 0; i < 3; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(100*i, 0, 100, 29);
        [but setImage:[UIImage imageNamed:[NSString stringWithFormat:@"seg%d_back",i]] forState:0];
        [but setImage:[UIImage imageNamed:[NSString stringWithFormat:@"seg%d_select",i]] forState:UIControlStateSelected];
        [but setTitleColor:[UIColor whiteColor] forState:0];
        [but setTitleColor:RGB(203, 74, 69) forState:UIControlStateSelected];
        [but addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
        but.tag = i+10010;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 29)];
        label.text = arr[i];
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = i+10015;
        label.backgroundColor = [UIColor clearColor];
        [but addSubview:label];
        [titleView addSubview:but];
    }
    self.navigationItem.titleView = titleView;
    self.navigationItem.hidesBackButton = YES;
    [self segmentAction:(UIButton *)[titleView viewWithTag:10010]];
    _tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain]retain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionIndexColor = RGB(198, 196, 196);
    [_tableView registerClass:[ChoosePetsCell class] forCellReuseIdentifier:@"pet"];
    [self.view addSubview:_tableView];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [[self iigetPetList]firstPage];
    [_tableView addHeaderWithCallback:^{
        [[self iigetPetList]firstPage];;
    }];
    
}



-(void)segmentAction:(UIButton *)sender{
    NSLog(@"count - %ld",(long)sender.tag);
    currentindex = sender.tag%10010;
    for (id obj in titleView.subviews) {
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *but = (UIButton *)obj;
            but.selected = NO;
        }
    }
    sender.selected = !sender.selected;
    for (id obj in titleView.subviews) {
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *but = (UIButton *)obj;
            if (but.selected) {
                UILabel *label = (UILabel *)[titleView viewWithTag:but.tag+5];
                label.textColor = RGB(203, 74, 69);
            }else{
                UILabel *label = (UILabel *)[titleView viewWithTag:but.tag+5];
                [label setTextColor:[UIColor whiteColor]];
            }
        }
    }
    [_tableView reloadData];
//    [self cacDataArray];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.105)];
    v.backgroundColor = [UIColor whiteColor];
    v.alpha = 0.8;
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
    NSMutableArray *currentArr = _dataArray[self.segment.selectedSegmentIndex];
    petsModel *temp = currentArr[section];
    label.textColor = RGB(247, 90, 83);
    label.font = [UIFont systemFontOfSize:30];
    label.text = temp.key;
    [v addSubview:label];
    UIView *hline =[[UIView alloc]initWithFrame:CGRectMake(0, v.frame.size.height-1, [UIScreen mainScreen].bounds.size.width+100, 1)];
    hline.backgroundColor = RGB(169, 167, 167);
    [v addSubview:hline];
    
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (_dataArray.count >0) {
        return  [_dataArray[currentindex]count];
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_dataArray.count >0) {
        NSMutableArray *currentArr = _dataArray[currentindex];
        petsModel *model = currentArr[section];
        return model.data.count;
    }else{
        return 0;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChoosePetsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pet"];
    petsModel *temp = _dataArray[currentindex][indexPath.section];
    PETMODEL *model = temp.data[indexPath.row];
    cell.nameLabel.text = model.name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.getpet) {
        NSMutableArray *currentArr = _dataArray[currentindex];
        petsModel *temp = currentArr[indexPath.section];
        PETMODEL *model = temp.data[indexPath.row];
        self.getpet(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}


//设置右侧的title;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    if (_dataArray.count >0) {
        
        for (int i = 0; i < [_dataArray[currentindex] count]; i++) {
            petsModel *model = _dataArray[currentindex][i];
            [temp addObject:model.key];
        }
        return temp;
    }else{
        return temp;
    }
}

-(void)getPetModelBlock:(getPetModel)block{
    self.getpet = [block copy];
}


ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

@end


